<template>
  <div class="page-container">
    <pf-table
      ref="pfTable"
      v-model="form"
      :columns="columns"
      :form-items="formItems"
      @get-table-data="getTableData"
    >
      <template #image="{row}">
        <el-image
          style="width: 100px; height: 100px"
          :src="row.coverImg"
          :preview-src-list="[row.coverImg]"
        />
      </template>
      <template #handle-area>
        <el-button type="primary" @click="addHandle">新增活动</el-button>
      </template>
      <template #handle="{row }">
        <el-button type="text" size="small" @click="gotoDetail(row)">详情</el-button>
        <el-button v-if="row.activityStatus === 1" type="text" size="small" @click="editHandle(row)">编辑</el-button>
        <!--        <el-button type="text" :class="row.activityStatus === 1 ? 'danger-text-button':''" size="small" @click="editStatus(row)">{{ row.activityStatus === 1 ? '下架' : '上架' }}</el-button>-->
        <el-button v-if="row.activityStatus === 0" type="text" class="danger-text-button" size="small" @click="deleteHandle(row)">删除</el-button>
        <!--        <el-button type="text" size="small" @click="showCode(row)">二维码</el-button>-->
      </template>
    </pf-table>
    <AddActivity
      v-if="showAddActivity"
      :id="activityId"
      :type="type"
      @close="closeHandle"
      @confirm="confirmHandle"
    />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import AddActivity from './AddActivity.vue'
import { ACTIVITY_STATUS } from '@/utils/enum'
import { getActivityList, editActivityStatus, deleteActivity } from '@/api/activity'

export default {
  name: 'ActivityList',
  components: {
    AddActivity
  },
  data() {
    return {
      type: 'add', // add:新增  edit：编辑  detail:详情
      statusOptions: ACTIVITY_STATUS,
      activityId: '',
      form: {},
      loading: false,
      showAddActivity: false
    }
  },
  computed: {
    ...mapGetters([
      'currentEnterprise',
      'currentSite'
    ]),
    columns() {
      const attrs = { 'min-width': '100px' }
      return [
        { label: '封面图片', slot: 'image', attrs: { width: '150px' }},
        { label: '标题', prop: 'title', attrs },
        { label: '活动状态', prop: 'activityStatus', attrs },
        { label: '报名人数', prop: 'enrollCount', attrs },
        { label: '是否收费', prop: 'isFee', formatter: 'money', attrs },
        { label: '状态', prop: 'effectiveStatus', attrs },
        { label: '创建时间', prop: 'createTime', attrs },
        { label: '创建人', prop: 'createUser', attrs },
        { label: '操作', slot: 'handle', attrs: { width: '200px', fixed: 'right' }}
      ]
    },
    formItems() {
      return [
        {
          type: 'input',
          label: '标题',
          prop: 'title'
        }, {
          type: 'select',
          label: '活动状态',
          prop: 'activityStatus',
          options: this.statusOptions
        }, {
          type: 'date',
          label: '创建时间',
          prop: 'dateRange',
          span: 8,
          attrs: {
            type: 'datetimerange'
          }
        }
      ]
    }
  },
  created() {},
  methods: {

    getTableData({ pageNo, pageSize, ...formQuery }, done) {
      this.loading = true
      const [createStartTime = '', createEndTime = ''] = formQuery?.dateRange || []
      const data = {
        pageNo,
        pageSize,
        createStartTime,
        createEndTime,
        siteId: this.currentSite.id,
        ...formQuery
      }
      getActivityList(data).then(res => {
        this.loading = false
        done({ data: res.data || [], total: res.count || 0 })
      }).catch(() => {
        done({ data: [], total: 0 })
        this.loading = false
      })
    },

    // 修改状态
    editStatus(row) {
      const data = {
        id: row.id,
        status: row.activityStatus === 1 ? 0 : 1
      }
      let message = ''
      let tip = ''
      if (row.activityStatus === 0) {
        message = '确定要上架该活动吗？'
        tip = '上架活动成功'
      } else {
        message = '确定要下架该活动吗？'
        tip = '下架活动成功'
      }
      this.$confirm(message, '提示', {
        type: 'warning'
      }).then(() => {
        editActivityStatus(data).then(() => {
          this.$message.success(tip)
          this.$refs.pfTable.getTableData()
        }).catch(() => {})
      })
    },

    // 新增
    addHandle() {
      this.type = 'add'
      this.activityId = ''
      this.showAddActivity = true
    },

    // 详情
    gotoDetail(row) {
      this.$router.push({
        path: '/marketing/activity/detail',
        query: {
          id: row.id
        }
      })
    },

    // 编辑
    editHandle(row) {
      this.type = 'edit'
      this.activityId = row.id
      this.showAddActivity = true
    },

    // 删除
    deleteHandle(row) {
      const data = {
        id: row.id
      }
      this.$confirm('确定删除该活动吗?', '提示', {
        type: 'warning'
      }).then(() => {
        deleteActivity(data).then(() => {
          this.$message.success('删除成功')
          this.$refs.pfTable.getTableData()
        }).catch(() => {})
      }).catch(() => {})
    },

    // 展示二维码
    showCode(row) {

    },

    // 关闭弹窗
    closeHandle() {
      this.showAddActivity = false
    },

    // 确认
    confirmHandle() {
      this.$refs.pfTable.getTableData()
    }
  }
}
</script>

<style lang="scss" scoped></style>
